package email;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 * Allows the user to select which email in a thread that they want to reply to
 * @author Jamison
 */
public class ReplyBox extends JDialog{
	
	/**
	 * The button that closes the reply box
	 */
	JButton cancel = new JButton("Cancel");
	
	/**
	 * The button that opens the Message Dialog when a message is selected
	 */
	JButton select = new JButton("Select");
	
	/**
	 * Table containing the messages
	 */
	JTable table;
	
	/**
	 * The table model used in constructing table
	 */
	CustomMessageTableModel model;
	
	JFrame area;
	
	/**
	 * Creates the reply box for user to select an email from
	 * @param t the thread that the user selected reply on
	 * @param forDislplay
	 */
	public ReplyBox(Thread t, JFrame forDisplay){
		area = forDisplay;
		
		//create frame
		final JFrame frame = new JFrame("Reply Selection");
		//create panels for content
		JScrollPane scroll = new JScrollPane();
		JPanel messages = new JPanel();
		JPanel buttonPanel = new JPanel();
		JPanel infoPanel = new JPanel();
		//set size of the content panels
		frame.setSize(500, 500);
		messages.setSize(new Dimension(500, 500));
		scroll.setSize(new Dimension(500, 500));

		
		//create buttons
		buttonPanel.add(select);
		buttonPanel.add(cancel);
		buttonListeners(frame);
		
		//create instructions
		JLabel label = new JLabel("Please select the email you wish to reply to.");
		infoPanel.add(label);
		
		//get messages and create table model
		ArrayList<ReceivedMessage> arr = t.getEmails();
		model = new CustomMessageTableModel();
		model.setMessages(arr);
		
		//set up table
		table = new JTable(model);
		table.setSize(new Dimension(500, 500));
		//messages.add(table);
		scroll.add(table);
		messages.add(scroll);
		
		//set up view so table appears
		scroll.setViewportView(table);
		scroll.validate();
		messages.validate();
		table.validate();
		
		//add items to frame
		frame.add(messages, BorderLayout.CENTER);
		frame.add(buttonPanel, BorderLayout.SOUTH);
		frame.add(infoPanel, BorderLayout.NORTH);
		table.setVisible(true);
		messages.setVisible(true);
		scroll.setVisible(true);
		frame.setVisible(true);
	}
	
	/**
	 * Attachs action listeners to the select and cancel button
	 * @param frame
	 */
	private void buttonListeners(final JFrame frame){
		//close the pop up when cancel is selected
		cancel.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				frame.dispose();
			}
		});
		
		//brings up message dialog when the select button is pressed
		select.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				//show error and exit if nothing is selected
				if (table.getSelectedRowCount() == 0){
					JOptionPane.showMessageDialog(getRootPane(), "Nothing is selected to reply to!");
					return;
				}
				//show error if multiple emails are selected
				else if (table.getSelectedRowCount() > 1){
					JOptionPane.showMessageDialog(getRootPane(), "Can't reply to multiple items at one time!");
					return;
				}
				
				//open up the reply dialog
				ReceivedMessage m = model.getMessage(table.getSelectedRow());
				MessageDialog dialog = new MessageDialog(area, 1, m);
				dialog.setVisible(true);
			}
		});
	}
}
